import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams,ViewController } from 'ionic-angular';
import { FormBuilder, Validators, FormGroup } from '@angular/forms';
import {UsersService} from '../../providers/users.service';
import {HousesService} from '../../providers/houses.service';
import {FacePage} from '../face/face';
import {Storage} from '@ionic/storage';
/**
 * Generated class for the OwnerPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-owner',
  templateUrl: 'owner.html',
})
export class OwnerPage {

  // userInfo: any;
  ownerForm: any;
  areaName:any;
  address:any;
  floor:any;
  area:any;
  aspect:any;
  housePrice:any;
  houseName:any;
  houseTypeId:any;

  constructor(
    private viewCtrl: ViewController,
    private storage: Storage,
    private formBuilder: FormBuilder,
    private userSer:UsersService,
    public navCtrl: NavController,
    public navParams: NavParams,
    private hoSer:HousesService,
  ) {

    // this.userInfo = this.params.get('userInfo');
    // console.log(this.userInfo);
    this.ownerForm = this.formBuilder.group({

      areaName: ['', Validators.compose([Validators.required])],
      address: ['', Validators.compose([Validators.required])],
      floor: ['', Validators.compose([Validators.required])],
      area: ['', Validators.compose([Validators.required])],
      aspect: ['', Validators.compose([Validators.required])],
      housePrice: ['', Validators.compose([Validators.required,  Validators.pattern("^\d{3,6}$")])],
      houseName: ['', Validators.compose([Validators.required, Validators.pattern("[^\x00-\xff]{10,20}$")])],
      houseTypeId: ['', Validators.compose([Validators.required])],
    });

    this.areaName = this.ownerForm.controls['areaName'];
    this.address = this.ownerForm.controls['address'];
    this.floor = this.ownerForm.controls['floor'];
    this.area = this.ownerForm.controls['area'];
    this.aspect = this.ownerForm.controls['aspect'];
    this.housePrice = this.ownerForm.controls['housePrice'];
    this.houseName = this.ownerForm.controls['houseName'];
    this.houseTypeId = this.ownerForm.controls['houseTypeId'];

  }
  userId;
  ionViewDidLoad() {
    // console.log('ionViewDidLoad OwnerPage');
    let that = this;
    that.storage.ready().then(() =>{
      that.storage.get('user').then((user) =>{
        // console.log(user);
        that.userId = user.userId;
      });

    })
  }

  houseId:number = 1;
  owner_res:string;
  onSubmit(owner) {
    // let owner = ownerForm.form.value;
    let that = this;


    owner.ownerId = that.userId;
    console.log(owner);
    // that.navCtrl.push(FacePage,{"houseId":67});


    that.hoSer.addHouse(owner, function (result) {
      // 弹出接收后台数据数值

      // alert(result.stateCode);
      if (result.stateCode == 11) {
        that.houseId = result.houseId;
        console.log(that.houseId);
        that.navCtrl.push(FacePage,{"houseId":that.houseId});

      } else {

        that.owner_res = '提交失败，请稍后再试';
      }
    })
  }
}
